package com.woniu.cms.goods.core.controller;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.woniu.cms.goods.core.entity.GoodsDictionary;
import com.woniu.cms.goods.core.service.IGoodsDictionaryService;
import com.woniu.cms.goods.core.service.IGoodsTypeService;
import com.woniu.common.api.Result;
import com.woniu.common.api.status.Impl.BusinessCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 材料字典表，内涵所有材料，所有组织机构共享 前端控制器
 * </p>
 *
 * @author romantic sheep
 * @since 2022-09-17
 */
@RestController
@Api(tags = "材料字典")
@RequestMapping("/goods-dictionary")
public class GoodsDictionaryController {

    @Autowired
    private IGoodsDictionaryService dictionaryService;
    @Autowired
    private IGoodsTypeService typeService;
    /*
     * 材料字典分类查询方法，前端根据材料类别分别展示材料详情
     * @currentPage 当前页码
     * @pageSize 页面大小
     * @dicId 材料类别id
     * */
    @GetMapping("list")
    @ApiOperation("分页查询/按类别查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage",value = "当前页码"),
            @ApiImplicitParam(name = "pageSize",value = "页面大小"),
            @ApiImplicitParam(name = "typeId",value = "材料类别id")
    })
    public Result getlist(Integer currentPage, Integer pageSize,Integer typeId,Integer status){
        Result result = dictionaryService.getlist(currentPage,pageSize,typeId,status);
        return result;
    }
    /*
    * 添加材料字典
    * */
    @PostMapping("insert")
    @ApiOperation("添加字典材料")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsDictionary",value = "添加字典对象")
    })
    public Result addDictionary(@RequestBody GoodsDictionary goodsDictionary) throws JsonProcessingException {
       Result result = dictionaryService.addDictionary(goodsDictionary);
       return result;
    }
    /*
    * 按id删除
    * */
    @DeleteMapping("delete")
    @ApiOperation("按id删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "删除对象id")
    })
    public Result deleteDictionary(Integer id){
        boolean b = dictionaryService.removeById(id);
        if (!b){
            return Result.error(BusinessCode.REMOVE_FAIL);
        }
        return Result.error(BusinessCode.REMOVE_SUCCESS);
    }
    /*
    * 批量删除
    * @ids，需删除的对应id组成的集合
    * */
    @DeleteMapping("deleteAll")
    @ApiOperation("批量删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids",value = "需删除的对应id组成的集合")
    })
    public Result deleteAll(@RequestBody List<Integer> ids){
        boolean b = dictionaryService.removeBatchByIds(ids);
        if (!b){
            return Result.error(BusinessCode.REMOVE_FAIL);
        }
        return Result.error(BusinessCode.REMOVE_SUCCESS);
    }
    /*
    * 修改字典
    * */
    @PutMapping("update")
    @ApiOperation("修改字典材料")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsDictionary",value = "修改的对象")
    })
    public Result updateDictionary(GoodsDictionary goodsDictionary){
        Result result = dictionaryService.updateDictionary(goodsDictionary);
        return result;
    }

}

